import { getSignData, editTweeterRemark } from '@src/api/loan'
Component({
  data: {
    tabCur: 0, //默认选中
    tabs: ['全部', '待跟进', '跟进中', '已成单', '已取消'],
    list: [],
    hasMoreData: false,
    queryPar: {
      followStatus: '',
      page: 1,
      pageSize: 10,
    },
    showRemark: false,
    markPar: {
      id: '',
      tweeterRemark: '',
    },
  },
  methods: {
    tabSelect(e) {
      this.setData({
        tabCur: e.currentTarget.dataset.id,
        scrollLeft: (e.currentTarget.dataset.id - 2) * 200,
        hasMoreData: false,
      })
      wx.startPullDownRefresh()
    },
    onLogin() {
      let _that = this
      wx.checkSession({
        success() {},
        fail() {
          _that.selectComponent('#login')._login(true)
        },
      })
    },
    onReady() {
      this.getList()
    },
    onAnon() {
      this.setData({ login: false })
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
      wx.showNavigationBarLoading()
      wx.showLoading({
        title: '加载中...',
      })
      const that = this
      that.setData({ 'queryPar.page': 1, list: [], hasMoreData: false })
      this.getList()
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
      let { page } = this.data.queryPar
      if (this.data.hasMoreData) {
        return
      } else {
        page++
      }
      wx.showLoading({ title: '正在拼命加载' }) // 加载转圈显示
      const that = this
      that.setData({ onRefresh: true, 'queryPar.page': page }) // 累加数据
      this.getList()
    },
    getList() {
      setTimeout(() => {
        let { tabCur, queryPar, list } = this.data
        queryPar.followStatus = tabCur - 1 < 0 ? '' : tabCur - 1
        getSignData(queryPar).then((result) => {
          let { data: res } = result
          if (result.code == 0) {
            this.setData({ list: [...list, ...res.list] })
          }
          if (!res.hasNextPage) {
            this.setData({ hasMoreData: !res.hasNextPage })
          }
          wx.hideLoading()
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
        })
      }, 1000)
    },
    editRemark(e) {
      let {
        item: { id, tweeterRemark },
      } = e.currentTarget.dataset
      this.setData({
        markPar: { id, tweeterRemark },
        showRemark: true,
      })
    },
    closeMark() {
      this.setData({
        markPar: { id: '', tweeterRemark: '' },
        showRemark: false,
      })
    },
    textInput(e) {
      let { value } = e.detail
      this.setData({
        'markPar.tweeterRemark': value,
      })
    },
    saveRemark() {
      editTweeterRemark(this.data.markPar).then((res) => {
        if (res.code == 0) {
          wx.showToast({ title: '修改备注成功', icon: 'none' }).then(() => {
            this.closeMark()
            wx.startPullDownRefresh()
          })
        }
      })
    },
  },
})
